package leetcode.code2411;

import leetcode.helper.H;

public class Solution2 {
	public int[] smallestSubarrays(int[] nums) {
		int[] ans = new int[nums.length];
		int[] h = new int[32];
		for (int i = nums.length - 1, limit = 0, r = nums.length - 1; i >= 0; i--) {
			int num = nums[i];
			int p = 0;
			while (num > 0) {
				if ((num & 1) == 1) {
					h[p]++;
					if (h[p] == 1) {
						limit |= 1 << p;
					} else if (h[p] > 1 && (limit & (1 << p)) != 0) {
						limit ^= 1 << p;
					}
				}
				num >>= 1;
				p++;
			}
			while (r > i && ((num = nums[r]) & limit) == 0) {
				p = 0;
				while (num > 0) {
					if ((num & 1) == 1) {
						h[p]--;
						if (h[p] == 1) {
							limit |= 1 << p;
						}
					}
					num >>= 1;
					p++;
				}
				r--;
			}
			ans[i] = r - i + 1;
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.smallestSubarrays(H.str2array3("[0]"));
		so.smallestSubarrays(H.str2array3("[9,5,0,10,7,2,9,2,4]"));
		System.out.println(Integer.toBinaryString(14));
	}
}
